package com.zhang.common.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * LocalFileManager
 *
 * @author zhangxin
 * @date 2021/6/18
 */
@Component
public class LocalFileManager implements IFileManager {

    private static final Logger logger = LoggerFactory.getLogger(LocalFileManager.class);


    public LocalFileManager() {
    }


    @Override
    public String put(MultipartFile multipartFile, String destSliceFile) throws IOException {
        if (destSliceFile == null) {
            throw new IOException("没有输入目标地址！");
        } else {
            File target = new File(destSliceFile);
            if (target.getParentFile() != null && !target.getParentFile().exists()) {
                target.getParentFile().mkdirs();
            }
            try {
                multipartFile.transferTo(target);
                return destSliceFile;
            } catch (IOException e) {
                logger.error(e.toString(), e);
            }

            return "";
        }
    }

    public String put(String localFile, String destSliceFile) throws IOException {
        if (destSliceFile == null) {
            throw new IOException("没有输入目标地址！");
        } else {
            File _file = new File(localFile);
            File target = new File(destSliceFile);
            if (target.getParentFile() != null && !target.getParentFile().exists()) {
                target.getParentFile().mkdirs();
            }

            boolean bl = moveFile(_file, target);
            return bl ? destSliceFile : "";
        }
    }


    @Override
    public boolean exist(String destSliceFile) throws IOException {
        if (destSliceFile == null) {
            throw new IOException("没有输入目标地址！");
        } else {
            File f = new File(destSliceFile);
            return f.exists();
        }
    }

    public InputStream get(String destFile) throws IOException {
        return null;
    }

    public static boolean moveFile(File src, File dest) {
        if (src != null && dest != null) {
            File dir = dest.getParentFile();
            if (dir != null && !dir.exists()) {
                dir.mkdirs();
            }

            byte[] buff = new byte[512];
            InputStream is = null;
            FileOutputStream os = null;

            try {
                is = new FileInputStream(src);
                os = new FileOutputStream(dest, false);
                int loopTimes = 0;

                int len;
                while((len = is.read(buff)) > 0) {
                    os.write(buff, 0, len);
                    ++loopTimes;
                    if (loopTimes > 0 && loopTimes / 1024 == 0) {
                        os.flush();
                    }
                }
            } catch (Exception var20) {
                var20.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (Exception var19) {
                }

                try {
                    if (os != null) {
                        os.close();
                    }
                } catch (Exception var18) {
                }

            }

            return src.delete();
        } else {
            return false;
        }
    }

}